/*
*线性表转置数据
*vs2010编译通过
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define elementType int

typedef struct
{
	int usedLength;
	int maxLength;
	elementType *elem;
} Sqlist;

int swatch(elementType *,elementType *);
int ifSuccess(int );
Sqlist createNewList();
int initElement(Sqlist *);
int setElement(int ,elementType,Sqlist *);
int insertAElement(int ,elementType ,Sqlist *);
int printAllElement(Sqlist *);
int getListLength(Sqlist *);
int deleteElement(int ,Sqlist *);
int enlargeList(Sqlist *);
int findElement(elementType,Sqlist *);
elementType findId(int ,Sqlist *);
int deleteList(Sqlist *);
int ifListExist(Sqlist *);

int main(int argc,char *argv[])
{
	//初始化线性表
	//start
	Sqlist list = createNewList();
	printf("请输入数据，空格隔开，Ctrl+d结束：\n");
	if(initElement(&list) == 0)
	{
		printf("初始化线性表成功\n");
	}else{
		printf("初始化线性表失败\n");
		return 0;
	}
	//end
	if(ifListExist(&list))
	{
		ifSuccess(printAllElement(&list));
		//转置数据
		//start
		int i;
		for(i = 0;i < list.usedLength/2;i ++)
		{
			swatch(&list.elem[i],&list.elem[list.usedLength - i - 1]);
		}
		//end
		ifSuccess(printAllElement(&list));
	}
	system("pause");
	return 0;
}

int swatch(elementType *i,elementType *j)
{
	elementType tempElement = *i;
	*i = *j;
	*j = tempElement;
	return 0;
}
int ifSuccess(int s)
{
	if(s == 0)
	{
		printf("--操作成功--\n");
	}else
	{
		return s;
	}
	return 0;
}

Sqlist createNewList()
{
	Sqlist list;
	list.usedLength = 0;
	list.maxLength = 100;
	list.elem = (elementType *)malloc(sizeof(elementType)*list.maxLength);
	return list;
}

int initElement(Sqlist *list)
{
	int start = 0;
	elementType element;
	while(scanf("%d",&element))
	{
		if(start == list -> maxLength)
		{
			if(enlargeList(list) != 0)
			{
				printf("扩充线性表失败，程序退出\n");
				return -1;
			}
		}
		list -> elem[start] = element;
		list -> usedLength ++;
		start ++;
	}
	getchar();
	return 0;
}

int insertAElement(int id,elementType element,Sqlist *list)
{
	int i;
	if(id >= 0 && id <= list -> usedLength)
	{
		if(id == list -> maxLength)
		{
			if(enlargeList(list) != 0)
			{
				printf("扩充线性表失败，程序退出\n");
				return -1;
			}
		}
		for(i = list -> usedLength;i > id;i --)
		{
			list -> elem[i] = list -> elem[i - 1];
		}
		list -> elem[id] = element;
		list -> usedLength ++;
		return 0;
	}else
	{
		return 1;
	}
}

int setElement(int id,elementType element,Sqlist *list)
{
	if(id >= 0 && id < list -> usedLength)
	{
		list -> elem[id] = element;
		return 0;
	}else
	{
		return 1;
	}
	return 0;
}

int printAllElement(Sqlist *list)
{
	int i;
	for(i = 0;i < list -> usedLength;i ++)
	{
		printf("%d ",list -> elem[i]);
	}
	return 0;
}

int getListLength(Sqlist *list)
{
	return list -> usedLength;
}

int deleteElement(int id,Sqlist *list)
{
	if(id >= 0 && id < list -> usedLength)
	{
		int i;
		for(i = id;i < list -> usedLength - 1;i ++)
		{
			list -> elem[i] = list -> elem[i + 1];
		}
		list -> usedLength --;
	}else
	{
		return 1;
	}
	return 0;
}

int enlargeList(Sqlist *list)
{
	list -> elem = (elementType *)realloc(list -> elem,sizeof(elementType)*(list -> maxLength + 100));
	if(!list -> elem)
	{
		return -1;
	}else
	{
		list -> maxLength += 100;
	}
	return 0;
}	

int findElement(elementType find,Sqlist *list)
{
	int i;
	for(i = 0;i < list -> usedLength;i ++)
	{
		if(list -> elem[i] == find)
		{
			return i;
		}
	}
	return -1;
}

elementType findId(int id,Sqlist *list)
{
	return list -> elem[id];
}

int deleteList(Sqlist *list)
{
	if(list -> maxLength != 0)
	{
		free(list -> elem);
		list -> elem = NULL;
		list -> usedLength = 0;
		list -> maxLength = 0;
		return 0;
	}else
	{
		return 1;
	}
	return 1;
}

int ifListExist(Sqlist *list)
{
	if(list -> maxLength != 0)
	{
		return 1;
	}else
	{
		return 0;
	}
	return 0;

}